<?php

// +-------------------------------------------------------
// | Name: 星空框架 管理员验证规则
// +-------------------------------------------------------
// | Version: V5.0
// +-------------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-------------------------------------------------------
// | Time: 2021-04-21 16:56
// +-------------------------------------------------------
declare(strict_types=1);

namespace app\common\validate;

use app\common\model\Admin AS AdminModel;
use think\Validate;

/**
 * @xk 管理员验证规则
 */
class Admin extends Validate
{
    protected $rule = [
    'admin_id' => 'number|min:1',
    'admin_name' => 'require|min:2|max:100',
    'admin_num' => 'checkNum',
    'admin_pass' => 'checkPass',
    'admin_pass1' => 'checkPass1',
    'admin_email' => 'checkEmail',
    'admin_tel' => 'checkTel',
    'order_id' => 'number|min:1',
    'status' => 'number|min:1',
  ];

    protected $message = [
    'admin_id.number' => '请选择需要操作的管理员id',
    'admin_name.require' => '管理员昵称不能为空',
    'admin_num.checkNum' => '请输入不小于六位的字母或数字，设置账号|账号已经存在',
    'admin_pass.checkPass' => '请输入不小于六位的字母或数字，设置密码',
    'admin_pass1.checkPass1' => '请输入正确的确认密码',
    'admin_email.checkEmail' => '请输入正确的邮箱',
    'admin_tel.checkTel' => '请输入正确的手机号',
    'order_id.number' => '排序id参数有误',
    'status.number' => '状态参数有误',
  ];

    protected $scene = [
    'create' => ['admin_name', 'admin_num', 'admin_pass', 'admin_pass1', 'admin_email', 'admin_tel', 'order_id', 'status'],
    'edit' => ['admin_group_id', 'admin_name', 'admin_num', 'admin_pass', 'admin_pass1', 'admin_email', 'admin_tel', 'order_id', 'status'],
  ];
  /**
   * @xk 验证账号
   *
   * @var [type]
   */
  protected function checkNum($value){
    // 验证账号规则
    $rule = '/^[a-zA-Z0-9]+$/u';
    if(!preg_match($rule,$value)){
      return false;
    }
    $adminModel = new AdminModel();
    $param = request()->param();
    $act = isset($param['act'])?$param['act']:'create';
    $condition = [];
    $condition[] = ['admin_num', '=', $value];
    switch ($act) {
        case 'create':
          $count = $adminModel->where($condition)->count();
          if ($count > 0) {
              return false;
          }
          return true;
          break;
        case 'edit':
          $admin_id = $adminModel->where($condition)->value('admin_id');
          if($admin_id <= 0){
              return true;
          }
          if ($admin_id == $param['admin_id']) {
              return true;
          }
          return false;
          break;
    }
  }
  /**
   * @xk 验证密码
   *
   * @var [type]
   */
  protected function checkPass($value){
    $param = request()->param();
    // 验证账号规则
    $rule = '/^[a-zA-Z0-9]+$/u';
    if($param['act'] =='create' && !preg_match($rule,$value)){
      return false;
    }
    return true;
  }
  /**
   * @xk 验证重复密码
   *
   * @var [type]
   */
  protected function checkPass1($value){
    $param = request()->param();
    // 验证账号规则
    $rule = '/^[a-zA-Z0-9]+$/u';
    if($param['act'] =='create' && !preg_match($rule,$value)){
      return false;
    }
    if($value != $param['admin_pass1']){
      return false;
    }
    return true;
  }
  /**
   * @xk 验证邮箱是否满足规则
   *
   * @var [type]
   */
  protected function checkEmail($value){
    $rule = '/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/';
    if($value != '' && !preg_match($rule,$value)){
      return false;
    }
    return true;
  }
  /**
   * @xk 验证手机号
   *
   * @var [type]
   */
  protected function checkTel($value){
    $rule = '/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/';
    if($value != '' && !preg_match($rule,$value)){
      return false;
    }
    return true;
  }
}
